home *** CD-ROM | disk | FTP | other *** search
/ Apple II Magazines (PO) / Nibble Volume 10, No. 01 (1989-01)(MicroSPARC)(Side A)[a].zip / Nibble Volume 10, No. 01 (1989-01)(MicroSPARC)(Side A)[a].po / PLOAD.INSTALL.S < prev    next >
Text File  |  1996-12-24  |  15KB  |  464 lines

  1. *
  2. * PLOAD.INSTALL Source Code
  3. * BY STEVE ELLIS
  4. * COPYRIGHT (C) 1989
  5. * MICROSPARC, INC.
  6. * CONCORD, MA 01742
  7. *
  8.           XC             ;turn on 65816 opcodes
  9.           XC
  10.           ORG $4000      ;run at $4000
  11.  
  12. * ProDOS equates
  13. HIMEM     EQU $73        ;himem pointer
  14. EXTCMD    EQU $BE06      ;vector to external commands
  15. ERROUT    EQU $BE09      ;ProDOS error handler
  16. XTRNADR   EQU $BE50      ;external command address for BI
  17. XLEN      EQU $BE52      ;length of command string minus 1
  18. XCNUM     EQU $BE53      ;BASIC command number (0 if external)
  19. PBITS     EQU $BE54      ;BI parms to be parsed
  20. GOSYS     EQU $BE70      ;MLI interface
  21. FIFILID   EQU $BEB8      ;file ID type
  22. FIAUXID   EQU $BEB9      ;auxiliary file type
  23. SREFNUM   EQU $BEC7      ;GET_FILE_INFO reference number
  24. MARK      EQU $BEC8      ;in-file position mark
  25. OSYSBUF   EQU $BECE      ;buffer for OPEN
  26. OREFNUM   EQU $BED0      ;OPEN file reference number
  27. RWRFNUM   EQU $BED6      ;READ/WRITE file reference number
  28. RWDATA    EQU $BED7      ;pointer to data to be used
  29. RWCOUNT   EQU $BED9      ;number of bytes to read/write
  30. RWTRANS   EQU $BEDB      ;returned # of bytes read
  31. CREFNUM   EQU $BEDE      ;CLOSE file reference number
  32. GETBUFR   EQU $BEF5      ;ProDOS buffer allocation routine
  33. BITMAP    EQU $BF58      ;ProDOS system bit map
  34. IVERSION  EQU $BFFD      ;BI version number
  35. COUT      EQU $FDED      ;character out routine
  36.  
  37. *  Storage for program variables
  38. PTR       EQU $00
  39. UPKAR     EQU $02
  40. UPKSZ     EQU $06
  41.  
  42. *  Constants
  43. BLKSIZE   EQU $1000      ;size of each data block
  44.                          ;BLKSIZE MUST be an even divisor of $8000!
  45. SOURCE    EQU $00005000  ;source address of data block
  46. DEST      EQU $00E12000  ;super hi-res page
  47.  
  48. * General purpose macros:                            *
  49. *  Put the 65816 in emulation mode, 8 bit acc. and registers
  50. EMULATE   MAC
  51.           SEC
  52.           XCE
  53.           <<<
  54. *  Switch to native mode, 16 bit acc. and registers
  55. NATIVE    MAC
  56.           CLC
  57.           XCE
  58.           REP #$30
  59.           <<<
  60. *  Macro to simulate a branch to subroutine instruction
  61. BSR       MAC
  62.           PER *+5
  63.           BRL ]1
  64.           <<<
  65. *  Following are macros to perform MLI calls and     *
  66. *  file error handling:                              *
  67. FILERR    MAC
  68.           BCC *+5
  69.           BRL MLIERR
  70.           <<<
  71. GET_FILE_INFO MAC
  72.           LDA #$C4
  73.           JSR GOSYS
  74.           FILERR
  75.           <<<
  76. OPEN      MAC
  77.           LDA #$C8
  78.           JSR GOSYS
  79.           FILERR
  80.           <<<
  81. READ      MAC
  82.           LDA #$CA
  83.           JSR GOSYS
  84.           FILERR
  85.           <<<
  86. SET_MARK  MAC
  87.           LDA #$CE
  88.           JSR GOSYS
  89.           FILERR
  90.           <<<
  91.  
  92.           EMULATE
  93.           LDA EXTCMD+2   ;get page of other commands
  94.           CMP #$BE       ;there are none
  95.           BEQ GETROOM    ; so don't bother looking
  96.           STA SRCHNG+2   ;save the address in zero page
  97. SEARCH0   LDY #0         ;start at byte 0
  98. SEARCH    INY            ;bump that to byte 1
  99.           BEQ ALREADY    ; means we have a match
  100. SRCHNG    LDA $0000,Y    ;get a byte
  101.           CMP COMMAND+256,Y ;compare to our code
  102.           BEQ SEARCH     ;if equal, look some more
  103.           INC SRCHNG+2   ;otherwise look at next higher page
  104.           LDA SRCHNG+2
  105.           CMP #$9A       ;up to start of DOS yet?
  106.           BCC SEARCH0    ; no, search some more
  107.           BRA GETROOM    ; yes, skip installed message
  108.  
  109. *  Print an error message stating that PLOAD has already
  110. *  been installed and return to BASIC.
  111. ALREADY   LDY #0
  112. :1        LDA AINSTL,Y   ;get a character
  113.           BEQ :2         ;stop on 0
  114.           JSR COUT       ;print the char.
  115.           INY            ;finish message
  116.           BNE :1         ;always
  117. :2        RTS            ;back to BASIC
  118.  
  119. *  Ask ProDOS for room for our command.              *
  120. GETROOM   LDA HIMEM+1    ;get top of free memory
  121.           CLC
  122.           ADC #4         ; add to that the ProDOS general buffer
  123.           STA OHIMEM     ; save the result for updating the bitmap later
  124.           LDA #>CMDEND-COMMAND ;get number of pages for our command
  125.           INC            ;add one for total pages needed
  126.           JSR GETBUFR
  127.           BCC GOTBUF     ;got them
  128.           JMP ERROUT     ;otherwise exit with an error
  129.  
  130. *  Now that we've got the space, we relocate a few   *
  131. *  addresses, and move our code up to its new home.  *
  132. GOTBUF    STA REL1+2
  133.           STA REL2+2
  134. *  Update the system bitmap
  135. MRKPAGE   TAX            ;get page number into acc.
  136.           PHA            ;save it
  137.           LSR            ;shift it right a few times
  138.           LSR
  139.           LSR
  140.           TAY            ; to address byte in bitmap
  141.           TXA
  142.           AND #7         ;isolate bit position
  143.           TAX
  144.           LDA #0
  145.           SEC            ;mark the page with a 1 bit
  146. :1        ROR
  147.           DEX
  148.           BPL :1
  149.           ORA BITMAP,Y   ;mask with previous value
  150.           STA BITMAP,Y   ; and store it
  151.           PLA            ;get page number
  152.           INC            ;bump it
  153.           CMP OHIMEM     ;done all the pages?
  154.           BCC MRKPAGE    ; no, finish it up
  155.  
  156. *  Check for BASIC version 1.1
  157. CATMOD    LDA IVERSION   ;get BI version number
  158.           CMP #1         ;must be version 1.1 for catalog mods
  159.           BNE DCHAIN     ;don't change anything
  160.           LDA #$C0       ;replace IVR and INT file desciptors
  161.           STA $B98E      ; with PNT and PIC file descriptors
  162.           INC
  163.           STA $B98D
  164.           LDA #"P
  165.           STA $B9AF
  166.           STA $B9B2
  167.           LDA #"I
  168.           STA $B9B3
  169.           LDA #"C
  170.           STA $B9B4
  171.           LDA #"N
  172.           STA $B9B0
  173.           LDA #"T
  174.           STA $B9B1
  175.  
  176. *  Daisy-chain our command
  177. DCHAIN    NATIVE
  178.           LDA EXTCMD+1   ;get previous address of ext. commands
  179.            STA  CMDLINK+4  ; and save so we can jump to it
  180. REL1      LDA #COMMAND   ;put address of our command
  181.           STA EXTCMD+1   ; into external jump
  182.           LDA #CMDEND-COMMAND.$00FF ;get address of last byte of program
  183.           LDX #COMMAND   ;get source address
  184. REL2      LDY #$0000     ; and destination address
  185.           MVN $00,$00    ; and move the program up
  186.           EMULATE
  187.           RTS
  188. OHIMEM    DS 1           ;room for old himem value
  189.           DS \           ;skip to next page
  190.  
  191. *  Scan the input line for our command
  192. COMMAND   CLD            ;valid command handler identifier
  193.           PER PLOAD      ;push run-time address of string PLOAD
  194.           LDY #0         ;scan for command
  195.           TYX            ; on input line
  196. :1        LDA $200,X     ;get a char
  197.           INX
  198.           CMP #" "       ;skip blanks
  199.           BEQ :1
  200.           AND #$DF       ;convert lower case to upper
  201.           CMP (01,S),Y   ;compare char. to command string
  202.           BNE CMDLINK
  203.           INY
  204.           CPY #5         ;got the whole word?
  205.           BCC :1         ; no, keep looking
  206.           DEY
  207.           STY XLEN       ;put the len-1 in BI global page
  208.           STZ XCNUM      ;command code = 0 means external handler
  209.           NATIVE
  210.           PER DO_CMD     ;push address of command handler
  211.           PLA            ;find out what it is
  212.           STA XTRNADR    ; and let the BI know where it is
  213.           LDA #$0401     ;require pathname 1, allow slot & drive
  214.           STA PBITS      ; for BI parser
  215.           PLA            ;pull address of 'PLOAD' off the stack
  216.           EMULATE
  217.           CLC            ;let BI parse it
  218.           RTS
  219. CMDLINK   PLA            ;clean up the stack
  220.           PLA
  221.           SEC            ;not our command
  222.           JMP $0000      ; so jump to any other handlers
  223.  
  224. DO_CMD    NATIVE
  225.           PER SAVBUF     ;push run-time address of save area
  226.           LDY #6
  227. :1        LDA PTR,Y      ;get a zero page byte
  228.           STA (01,S),Y   ;save it
  229.           DEY
  230.           DEY
  231.           BPL :1         ;finish all the 8 bytes
  232.           PLA
  233.           LDA #SOURCE
  234.           STA PTR        ;point to source data area
  235.           STA RWDATA     ;tell MLI where to load data
  236.           LDA #DEST
  237.           STA UPKAR      ;pointer to super hi-res screen
  238.           LDA #BLKSIZE
  239.           STA RWCOUNT    ;read one data block at a time
  240.           STZ MARK       ;zero file mark (start at byte 0)
  241.           LDA #$00E1     ;hi word of super hi-res screen location
  242.           STA UPKAR+2
  243.           LDA HIMEM      ;set HIMEM address
  244.           STA OSYSBUF    ; as buffer for OPEN
  245.           EMULATE
  246.           LDA #$40       ;initialize super hi-res
  247.           TSB $C029      ; without changing its current status
  248.           OPEN
  249.           LDA OREFNUM    ;copy our reference number
  250.           STA RWRFNUM    ; to read/write,
  251.           STA CREFNUM    ; close, and
  252.           STA SREFNUM    ; get_info refnums
  253.           GET_FILE_INFO
  254.           LDA FIFILID    ;check file ID type
  255.           CMP #$C1       ;full 32K image, no need to unpack
  256.           BEQ BIGPIC
  257.           CMP #$06       ;assume BIN files are 32K images
  258.           BEQ BIGPIC
  259.           CMP #$C0       ;packed picture image
  260.           BEQ PACPIC
  261. BADTYPE   LDA #$0D       ;FILE TYPE MISMATCH
  262.           BRA MLIERR     ;exit with error back to BASIC
  263.  
  264. CLOSE     EMULATE
  265.           LDA #$CC       ;CLOSE the file
  266.           JSR GOSYS
  267.           PER SAVBUF
  268.           LDY #7
  269. :1        LDA (01,S),Y   ;restore the ZP we trampled
  270.           STA PTR,Y
  271.           DEY
  272.           BPL :1
  273.           PLA
  274.           PLA
  275.           RTS            ;return to BASIC
  276.  
  277. MLIERR    PHA            ;save acc.
  278.           BSR CLOSE      ;close the file
  279.           PLA            ;get acc.
  280.           JMP ERROUT     ;abort
  281.  
  282. *  Load 32K images.                                  *
  283. BIGPIC    EMULATE
  284.           READ
  285.           NATIVE
  286.           PHB            ;save data bank
  287.           LDA #BLKSIZE-1 ;move one data block
  288.           LDX PTR        ; from source address
  289.           LDY UPKAR      ; to super hi-res page
  290.           MVN SOURCE,DEST
  291.           PLB            ;restore data bank
  292.           LDA UPKAR      ;find location on super hi-res page
  293.           CLC
  294.           ADC #BLKSIZE   ;increment screen pointer by size of block
  295.           STA UPKAR
  296.           CMP #$A000     ;done with the picture (up to $A000)?
  297.           BNE BIGPIC     ; no, do some more
  298.           BRA CLOSE      ;close the file and exit
  299.  
  300. PACPIC    NATIVE
  301.           LDA FIAUXID    ;get file AUX type
  302.           BEQ TYPE00
  303.           CMP #0001
  304.           BEQ TYPE01
  305.           CMP #0002
  306.           BEQ T02JMP
  307.           EMULATE
  308.           BRL BADTYPE    ;not a recognized packed file type
  309. T02JMP    BRL TYPE02     ;can't reach it with a normal branch
  310.  
  311. *  Load and unpack aux type $00 files.               *
  312. TYPE00    NATIVE
  313.           LDA #$7D00     ;only interested in screen data for unpacking
  314.           STA UPKSZ
  315.           LDA #$0020     ;read the palette
  316.           STA RWCOUNT
  317.           EMULATE
  318.           READ
  319.           LDX #0
  320.           TXA
  321. SCBLP0    STAL $E19D00,X ;zero out the scan line area, since all
  322.           INX            ; Paintworks pictures are 320 mode, palette 0
  323.           BNE SCBLP0
  324.           LDX #$1F
  325. PALTLP0   LDA SOURCE,X
  326.           STAL $E19E00,X ;move palette to palette area
  327.           DEX
  328.           BPL PALTLP0
  329.           NATIVE
  330.           LDA #$222      ;position past palette in file
  331.           STA MARK
  332.           LDA #BLKSIZE
  333.           STA RWCOUNT
  334. T00LOOP   EMULATE
  335.           SET_MARK
  336.           READ
  337.           NATIVE
  338.           BSR UNPACK
  339.           LDA UPKAR
  340.           CMP #$9D00
  341.           BLT T00LOOP
  342.           BRL CLOSE
  343.  
  344. *  Load and unpack aux type $01 files.               *
  345. TYPE01    NATIVE
  346.           LDA #$8000     ;SHR pic is $8000 bytes long
  347.           STA UPKSZ      ;tell toolbox
  348. T01LOOP   EMULATE
  349.           SET_MARK
  350.           READ
  351.           NATIVE
  352.           BSR UNPACK
  353.           LDA UPKSZ      ;unpacked the entire picture?
  354.           BNE T01LOOP    ; no, do some more
  355.           BRL CLOSE      ; else exit through CLOSE
  356.  
  357. *  Load and unpack aux type $02 files.               *
  358. TYPE02    NATIVE
  359.           LDA #$7D00     ;unpack only screen data (not SCB's, etc.)
  360.           STA UPKSZ
  361.           EMULATE
  362.           READ
  363.           NATIVE
  364.           LDA SOURCE+11  ;get number of horizontal pixels
  365.           CMP #320
  366.           BEQ PIXOK
  367.           CMP #640
  368.           BEQ PIXOK
  369.  
  370. *  If the picture doesn't have either 320 or 640 pixels, exit
  371. *  to BASIC with a RANGE ERROR.
  372.           EMULATE
  373.           LDA #2
  374.           BRL MLIERR
  375.  
  376. *  Continue unpacking after determining a standard screen width.
  377. *  First, copy the screen control byte for each scan line.
  378. PIXOK
  379.           MX 00
  380.           LDA SOURCE+9   ;get hi-byte of SCB byte
  381.           XBA            ;move it to high-byte of acc
  382.           ORA SOURCE+9   ; and get it in low-byte of acc
  383.           AND #$F0F0     ;only interested in high nibbles
  384.           LDX #0
  385. SCBLP2    STAL $E19D00,X ;put it in SCB storage area
  386.           INX
  387.           INX
  388.           CPX #$C8       ;only up to $E19DC7
  389.           BNE SCBLP2
  390.           LDA #0         ;zero out from $E19DC8 -> $E19DFF
  391. :1        STAL $E19D00,X
  392.           INX
  393.           INX
  394.           CPX #$100      ;done the whole page?
  395.           BNE :1         ; no, finish it up
  396.  
  397. *  Count the number of palettes and move them to where
  398. *  they belong (from $E19E00 up).
  399.           LDX SOURCE+13  ;index with number of palettes
  400.           LDA #$00       ;use acc. to hold address
  401. PALTLP2   CLC
  402.           ADC #$20       ;point to next palette (each is $20 bytes long)
  403.           DEX
  404.           BNE PALTLP2    ;more palettes
  405.           TAX            ;copy address of the end of the palettes into X
  406.           PHA            ; and save it
  407. :2        LDA SOURCE+15,X
  408.           STAL $E19E00,X ;move the data into palette area
  409.           DEX
  410.           DEX
  411.           BPL :2         ;more palette data
  412.  
  413. *  We have the start of the ScanLineDirectory now.  Skip
  414. *  over each entry (4 bytes) to find the beginning of the
  415. *  packed picture data.
  416.           PLY
  417.           TYA            ;get start addr. of entries in A
  418.           LDX SOURCE+15,Y ;get number of scan lines as index
  419. PICLP2    CLC
  420.           ADC #4         ;skip an entry
  421.           DEX
  422.           BNE PICLP2     ;more to do
  423.            ADC #17       ;adjust pointer to correct address
  424.           STA MARK       ;load from that point in file
  425.           LDA #BLKSIZE
  426.           STA RWCOUNT
  427. T02LOOP   EMULATE
  428.           SET_MARK
  429.           READ
  430.           NATIVE
  431.           BSR UNPACK
  432.           LDA UPKAR
  433.           CMP #$9D00
  434.           BLT T02LOOP
  435.           BRL CLOSE
  436.  
  437. *  Call the toolbox to unpack the picture.           *
  438. UNPACK    NATIVE
  439.           LDX RWTRANS    ;number of bytes actually read
  440.           LDA #0
  441.           PHA            ;space for result
  442.           PHA            ;pointer to buffer holding packed data
  443.           LDY PTR        ;low word of buffer
  444.           PHY
  445.           PHX            ;number of bytes read (size of packed data buffer)
  446.           PHA            ;pointer to pointer to
  447.           PEA #UPKAR     ; area to unpack into
  448.           PHA            ;pointer to word holding length
  449.           PEA #UPKSZ     ; of size of area to unpack into
  450.           LDX #$2703     ;tool number for UnPackBytes
  451.           JSL $E10000    ;call the toolbox
  452.           PLA            ;get number of bytes unpacked
  453.           CLC
  454.           ADC MARK       ;update the file mark by adding the number
  455.           STA MARK       ; of bytes actually unpacked to previous mark
  456.           RTS
  457.  
  458. PLOAD     ASC "PLOAD"
  459. AINSTL    HEX 8D
  460.           ASC "PLOAD ALREADY INSTALLED"
  461.           HEX 8D8D00
  462. SAVBUF    DS 8
  463. CMDEND    EQU *
  464.